package io.renren.modules.sys.service.impl;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.qcloud.cos.utils.StringUtils;
import io.renren.modules.sys.dao.QInvestigationDao;
import io.renren.modules.sys.dao.QQuestionDao;
import io.renren.modules.sys.entity.QInvestigation;
import io.renren.modules.sys.entity.QQuestion;
import io.renren.modules.sys.entity.QQuestionnaires;
import io.renren.modules.sys.service.QInvestigationService;
import jakarta.annotation.Resource;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * (QInvestigation)表服务实现类
 *
 * @author makejava
 * @since 2024-06-29 15:58:05
 */
@Service("qInvestigationService")
public class QInvestigationServiceImpl implements QInvestigationService {
    @Resource
    private QInvestigationDao qInvestigationDao;
    @Resource
    private QQuestionDao qQuestionDao;

    /**
     * 通过ID查询单条数据
     *
     * @param id 主键
     * @return 实例对象
     */
    @Override
    public QInvestigation queryById(String id) {
        return this.qInvestigationDao.queryById(id);
    }

    /**
     * 分页查询
     *
     * @param qInvestigation 筛选条件
     * @param pageRequest      分页对象
     * @return 查询结果
     */
    @Override
    public Page<QInvestigation> queryByPage(QInvestigation qInvestigation, PageRequest pageRequest) {
        long total = this.qInvestigationDao.count(qInvestigation);
        return new PageImpl<>(this.qInvestigationDao.queryAllByLimit(qInvestigation, pageRequest), pageRequest, total);
    }

    /**
     * 新增数据
     *
     * @param qInvestigation 实例对象
     * @return 实例对象
     */
    @Override
    public QInvestigation insert(QInvestigation qInvestigation) {
        this.qInvestigationDao.insert(qInvestigation);
        return qInvestigation;
    }

    /**
     * 修改数据
     *
     * @param qInvestigation 实例对象
     * @return 实例对象
     */
    @Override
    public QInvestigation update(QInvestigation qInvestigation) {
        this.qInvestigationDao.update(qInvestigation);
        return this.queryById(qInvestigation.getId());
    }

    /**
     * 通过主键删除数据
     *
     * @param id 主键
     * @return 是否成功
     */
    @Override
    public boolean deleteById(String id) {
        return this.qInvestigationDao.deleteById(id) > 0;
    }

    @Override
    public List<QQuestion> getQuestionsByInvestigationId(String investigationId) throws JsonProcessingException {
        List<QQuestion> questions = new ArrayList<>();
        QQuestionnaires qQuestionnaires = qInvestigationDao.getQuestionIdsByInvestigationId(investigationId);
        if (qQuestionnaires != null) {
            ObjectMapper mapper = new ObjectMapper();
            String questionsIdsStr = qQuestionnaires.getQuestions();
            String[] questionsIdsArray = mapper.readValue(questionsIdsStr, String[].class);
            questions = qQuestionDao.queryByIds(Arrays.asList(questionsIdsArray), questionsIdsStr);
        }
        return questions;
    }
}
